package cn.doit.scala.day04

import scala.collection.immutable
import scala.io.Source

/**
 * @DATE 2021/12/28/11:42
 * @Author MDK
 * @Version 2021.2.2
 * */
object C08_SameFriends02 {
  def main(args: Array[String]): Unit = {
    //读数据
    val lines: Iterator[String] = Source.fromFile("data/friends.txt").getLines()
    //处理数据封装在map集合中
    val list: List[(String, List[String])] = lines.map(line => {
      val arr = line.split(":")
      val name = arr(0)
      val friends = arr(1).split(",")
      (name, friends.toList)
    }).toList

    val res: immutable.Seq[(String, List[String])] = for(i<-0 until(list.length-1); j<-i+1 until(list.length))yield{
      val name: String = list(i)._1
      val fs1: List[String] = list(i)._2
      val name2: String = list(j)._1
      val fs2: List[String] = list(j)._2
      (s"$name 和 $name2 的共同好友:", fs1.intersect(fs2))
    }
    res.filter(_._2.length>0).foreach(println)
  }
}
